package com.trace.demo.config;

import com.trace.demo.entity.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 全局Web配置类
 * 配置跨域请求处理 - 完全允许所有跨域请求
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")  // 使用patterns替代origins以支持通配符
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
                .allowedHeaders("*")
                .exposedHeaders("*")  // 允许暴露的响应头
                .allowCredentials(true)  // 允许发送凭证信息
                .maxAge(3600);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加字符串到UserRole枚举的转换器
        registry.addConverter(String.class, User.UserRole.class, source -> User.UserRole.valueOf(source.toUpperCase()));
    }
} 